package com.quanquan.management.controller;

import base.BaseConst;
import base.BaseController;
import com.quanquan.management.dto.CouponDTO;
import com.quanquan.management.dto.MarketingRulesDTO;
import com.quanquan.management.request.MarketingRulesRequestBody;
import com.quanquan.management.service.CouponService;
import com.quanquan.management.service.MarketingRulesService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import web.response.ResultBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:新用户福利规则设置
 *
 * @ClassName: MarketingRulesController
 * @Author: xiaoqianh丶
 * @Date: 2019-05-24 17:15
 * @Version: V1.0
 */
@Controller
@RequestMapping("/marking_rule")
public class MarketingRulesController extends BaseController {

    private final CouponService couponService;
    private final MarketingRulesService marketingRulesService;

    @Autowired
    public MarketingRulesController(MarketingRulesService marketingRulesService, CouponService couponService) {
        this.marketingRulesService = marketingRulesService;
        this.couponService = couponService;
    }

    /**
     * 查看列表
     *
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model) {
        List<MarketingRulesDTO> list = marketingRulesService.list();
        model.addAttribute("pageObj",list);
        System.out.println(list);
        return "maketingRule/list";
    }

    /**
     * 编辑
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(MarketingRulesRequestBody requestBody, Model model) {

        List<CouponDTO> couponDTOS = couponService.couponList();
        model.addAttribute("couponDTOS", couponDTOS);
        List<CouponDTO> parkingCouponDTOS = couponService.parkingCouponList();
        model.addAttribute("parkingCouponDTOS", parkingCouponDTOS);

        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            MarketingRulesDTO byId = marketingRulesService.getById(requestBody.getId());
            System.out.println(byId);
            model.addAttribute("editable",byId);
            return "maketingRule/form";
        }
        boolean update = marketingRulesService.update(requestBody);
        if (update) {
            return "redirect:/marking_rule/list";
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("code",-1);
            result.put("msg","更新失败");
            model.addAllAttributes(result);
            return null;
        }
    }

    @GetMapping("/updateStatus")
    @ResponseBody
    public ResultBody updateStatus(@RequestParam String id, @RequestParam(required = false) boolean status) {
        MarketingRulesRequestBody requestBody = new MarketingRulesRequestBody();
        requestBody.setId(id);
        requestBody.setConsumptionQuota(status);
        boolean changeStatus = false;
        if (status == true) {
            changeStatus = false;
        }
        if (status == false) {
            changeStatus = true;
        }
        boolean b = marketingRulesService.updateStatus(requestBody);
        if (b) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),changeStatus);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
    }



}
